<?php

class form {

    protected static $eol = "\n";
    static public $uploadify = false;
    static public $editor = false;

    /**
     * 编辑器
     * @param string $type 编辑器类型
     * @param string $style 编辑器风格
     */
    public static function editor($name, $content = '', $toolbar = 'simple', $width = 800, $height = 300, $server = '/Admin/upload/image') {
        if (!self::$editor) {
            $str = '<script type="text/javascript" src="/Public/editor/umeditor/umeditor.config.js"></script>' . self::$eol;
            $str .= '<script type="text/javascript" src="/Public/editor/umeditor/umeditor.js"></script>' . self::$eol;
            $str .= '<script type="text/javascript" src="/Public/editor/umeditor/lang/zh-cn/zh-cn.js"></script>' . self::$eol;
            $str .= '<link rel="stylesheet" href="/Public/editor/umeditor/themes/default/css/umeditor.css">' . self::$eol;
            self::$editor = true;
        }
        $str .= <<<str
      <script id="container_{$name}" name="{$name}" type="text/plain" style="width:{$width}px;height:{$height}px;">{$content}</script>            
                
str;

        if ($toolbar == 'simple') {
            $str .= <<<str
<script>
	$(function(){
            window.{$name}_editor = UM.getEditor('container_{$name}', {              
                toolbar: [ 'undo redo | bold italic | superscript subscript | forecolor backcolor | removeformat |',
            ' selectall cleardoc paragraph | fontfamily fontsize' ,
            '| justifyleft justifycenter justifyright justifyjustify |','link unlink']
            });
        });

</script>                        
str;
        } else {
            $str .= <<<str
<script>
	$(function(){
            window.{$name}_editor = UM.getEditor('container_{$name}', {              
                imageUrl:"{$server}",
                imagePath:""
            });
        });

</script>                        
str;
        }
        return $str;
    }

    /**
     * 下拉选择框
     * @param array $data 数据
     * @param string $str 属性
     * @param string $selected 选中数据
     * @return string 
     */
    public static function select($data, $str, $selected = '', $default = '', $selected_type = "key") {
        if (!is_array($data)) {
            return '';
        }
        $input = "<select " . $str . ">" . self::$eol;
        if ($default != '') {
            $input .= '<option value="">' . $default . '</option/>' . self::$eol;
        }
        foreach ($data as $key => $val) {
            if (strtolower($$selected_type) === strtolower($selected)) {
                $input .= '<option value="' . $key . '" selected="selected" >' . $val . '</option>' . self::$eol;
            } else {
                $input .= '<option value="' . $key . '">' . $val . '</option>' . self::$eol;
            }
        }
        $input .= "</select>" . self::$eol;
        return $input;
    }

    /**
     * 单选按钮
     * @param array $data 数据
     * @param string $str 属性
     * @param string $selected 选中数据
     * @return string 
     */
    public static function radio($data, $str, $selected = '', $selected_type = "key") {
        if (!is_array($data)) {
            return '';
        }
        $input = '<div class="radio pull-left">' . self::$eol;
        foreach ($data as $key => $val) {
            if (strtolower($$selected_type) === strtolower($selected)) {
                $input .= <<<str
                    <label class="margin-right-20">
                        <input type="radio" {$str} value="{$key}" checked="checked">
                          <span class="text">{$val}</span>
                     </label>                        
str;
            } else {
                $input .= <<<str
                    <label class="margin-right-20">
                        <input  type="radio" {$str} value="{$key}" >
                          <span class="text">{$val}</span>
                     </label>                        
str;
            }
        }
        $input .= '</div>' . self::$eol;
        return $input;
    }

    /**
     * 多选按钮
     * @param array $data 数据
     * @param string $str 属性
     * @param mixed $selected 选中数据
     * @return string 
     */
    public static function checkbox($data, $str, $selected = '', $selected_type = "key") {
        if (!is_array($data)) {
            return '';
        }
        $input = '';
        $selected = (array) $selected;
        foreach ($data as $key => $val) {
            if (in_array($$selected_type, $selected)) {
                $input .= <<<str
                    <label class="margin-right-20">
                        <input type="checkbox" {$str} value="{$key}" checked="checked">
                          <span class="text">{$val}</span>
                     </label>                        
str;
            } else {
                $input .= <<<str
                    <label class="margin-right-20">
                        <input  type="checkbox" {$str} value="{$key}" >
                          <span class="text">{$val}</span>
                     </label>                        
str;
            }
        }
        return $input;
    }

}
